<?php
/**
 * @author zouhao
 *	qq:16852868
 *	zouhao619@gmail.com
 */
class PublicController extends Controller {
	/**
	 * 用户登陆
	 */
	public function login()
	{
	    if($this->isPost()){
	    	if($_SESSION['verify']!=md5($_POST['verify'])){
	    		exit(json_encode(array('status'=>2,'error_message'=>'验证码错误')));
	    	}
    		$map    =   array();
    		$map['username']	= $_POST['username'];
    		$admin=M('Admin')->where($map)->find();
    		if(empty($admin)){
    			exit(json_encode(array('status'=>3,'error_message'=>'不存在该账户')));
    		}else{
    			$role=M('Role')->field('is_forbid,name')->where("id={$admin['role_id']}")->find();
    			$admin['role_name']=$role['name'];
    			if($admin['password']!=md5($_POST['password'])){
    				exit(json_encode(array('status'=>4,'error_message'=>'密码错误!')));
    			}else if($admin['is_forbid']=="1"){
    				exit(json_encode(array('status'=>5,'error_message'=>'账户被禁用掉了')));
    			}else if($role['is_forbid']==1){
    				exit(json_encode(array('status'=>6,'error_message'=>'管理员所属角色被禁止了')));
    			}else{
    				//更新员工最后登录时间和ip
    				$data['id']=$admin['id'];
    				$data['last_login_time']=date('Y-m-d H:i:s');
    				$data['last_login_ip']=get_client_ip();
    				M('Admin')->update($data);
    				//更新菜单
    				$configmenuList=require CONFIG_PATH.'/config.menu.php';
    				$roleMenuList=M('RoleMenu')->field('menu_id',true)->where("role_id={$admin['role_id']}")->select();
    				
    				$menuList=$accessList=array();
    				foreach($configmenuList as $menu){
    					$m=array();
    					foreach($menu['children'] as $item){
    						if(in_array($item['id'],$roleMenuList)){
    							$accessList[$item['name']]=$item['url'];
    							if(!isset($item['is_hidden'])||$item['is_hidden']==false){
    								$m[]=$item;
    							}
    						}else if(isset($item['is_open'])&&$item['is_open']==true){
    							if(isset($item['is_hidden'])){
    								if($item['is_hidden']==false){
    									$m[]=$item;
    								}
    							}else{
    								$m[]=$item;
    							}
    							$accessList[$item['name']]=$item['url'];
    						}
    					}
    					if(!empty($m)){
    						$l['name']=$menu['name'];
    						$l['children']=$m;
    						$menuList[]=$l;
    					}
    				}
    				$_SESSION['accessList']=$accessList;
    				$_SESSION['menuList']=$menuList;
    				$_SESSION['admin']=$admin;
    				exit(json_encode(array('status'=>true)));
    			}
    		}
        }else{
            $this->display();
        }
	}
	Public function verify(){
		Image::buildImageVerify();
	}
	public function logout(){
		$_SESSION=null;
		session_destroy();
		$this->redirect('Public/login');
	}
	/**
	 * 上传缩略图
	 */
	public function upload() {
		$upload = new UploadFile (); // 实例化上传类
		$rs=array();
		if (! $upload->upload (UPLOAD_PATH.'/')) { // 上传错误提示错误信息
			$rs['status']=false;
			$rs['error_message']=$upload->getErrorMsg ();
		} else { // 上传成功
			$rs['status']=true;
			$rs['fileInfo']=$upload->getUploadFileInfo();
			$rs['fileInfo']=$rs['fileInfo']['0'];
		}
		echo json_encode($rs);
	}
}
